//
//  ARTWatchHistorySleepStageView.m
//  ChartsDemo
//
//  Created by yunda on 2021/11/18.
//

#import "ARTWatchHistorySleepStageView.h"

@interface ARTWatchHistorySleepStageView()

// start time
@property (strong, nonatomic)  UILabel *startLabel;
// end time
@property (strong, nonatomic)  UILabel *endLabel;

@end

@implementation ARTWatchHistorySleepStageView

-  (instancetype)init{
    self = [super init];
    if (!self) {
        return nil;
    }
    // init code
    self.frame = J_Frame(15, 0, J_SCREEN_WIDTH-15*2, 240);
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleView];
    
    J_ViewRadius(self, 10);
    return self;
}

- (void)startDrawing{
    CGFloat hour = _longTime*1.0/3600;
    NSInteger count = ceilf(hour)+1;
    CGFloat gap = 10;
    
    NSMutableArray *widthArray = [NSMutableArray array];
    CGFloat sumWidth = 0.0;
    for (int i = 0; i < count; i++) {
//        CGFloat w = [self widthOfLabelWithText:@(i).stringValue];
        CGFloat w = [self widthOfLabelWithText:[NSString stringWithFormat:@"%@h",@(i)]];
        sumWidth = sumWidth + w;
        [widthArray addObject:@(w)];
    }
    CGFloat labelGap = (self.v_w-gap*2-sumWidth)/(count-1);
    labelGap = labelGap <= 0 ? 0 : labelGap;
    CGFloat tempX = gap;
    for (int i = 0; i < count; i++) {
        NSNumber *width = widthArray[i];
        UILabel *label = [[UILabel alloc]initWithFrame:J_Frame(tempX, 50, width.floatValue, 20)];
        label.textColor = J_ColorFromHEX(0x333333);
        label.font = J_Font(12);
        label.adjustsFontSizeToFitWidth = YES;
        label.text = [NSString stringWithFormat:@"%@h",@(i)];//@(i).stringValue;
        [self addSubview:label];
        tempX = label.v_MaxX+labelGap;
    }
    
    NSArray *array = [_valueArray copy];
    CGFloat y = 50;
    CGFloat w = (self.v_w-gap*2)*(hour/(count-1))/array.count;
    CGFloat h = 120;
    for (int i = 0; i < array.count; i++) {
        NSNumber *type = array[i];
//        UIView *view = [[UIView alloc]init];
        CALayer *view = [[CALayer alloc]init];
        if (type.integerValue == 0) {
            // awake
            view.backgroundColor = J_ColorFromHEX(0x7c69B9).CGColor;
            view.frame = J_Frame(gap+w*i, y+(h-h/3), w, h/3);
        }else if (type.integerValue == 1){
            // light
            view.backgroundColor = J_ColorFromHEX(0x70DA462).CGColor;
            view.frame = J_Frame(gap+w*i, y+(h-h/2), w, h/2);
        }else if (type.integerValue == 2){
            // deep
            view.backgroundColor = J_ColorFromHEX(0x0095B6).CGColor;
            view.frame = J_Frame(gap+w*i, y+(h-h/1.5), w, h/1.5);
        }
//        [self addSubview:view];
        [self.layer addSublayer:view];
    }
    
    CGFloat buttonY = y+h+10;
    if (_timeArray.count >= 2) {
        for (int i = 0; i < 2; i++) {
            UILabel *label = [[UILabel alloc]initWithFrame:J_Frame(gap+(self.v_w-gap*2)/2*i, buttonY, (self.v_w-gap*2)/2, 20)];
            label.textColor = J_ColorFromHEX(0x333333);
            label.font = J_Font(12);
            if (i == 0) {
                label.textAlignment = NSTextAlignmentLeft;
                _startLabel = label;
                label.text = _timeArray.lastObject;
            }else{
                label.textAlignment = NSTextAlignmentRight;
                _endLabel = label;
                label.text = _timeArray.firstObject;
            }
            [self addSubview:label];
        }
        buttonY = buttonY+20;
    }
    
    UIButton *deepButton = [UIButton buttonWithType:UIButtonTypeCustom];
    deepButton.frame = J_Frame(gap-4, buttonY+10, 80, 10);
    [deepButton setTitle:FYLocalizedString(@"sleep_watch_deep_title", @"") forState:UIControlStateNormal];
    [deepButton setTitleColor:J_ColorFromHEX(0x333333) forState:UIControlStateNormal];
    deepButton.titleLabel.font = J_Font(12);
    [deepButton setImage:[UIImage qmui_imageWithColor:J_ColorFromHEX(0x0095B6) size:CGSizeMake(deepButton.v_h, deepButton.v_h) cornerRadius:0] forState:UIControlStateNormal];
    [deepButton jk_setImagePosition:LXMImagePositionLeft spacing:4];
    [self addSubview:deepButton];
    
    UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    lightButton.frame = J_Frame(deepButton.v_MaxX, deepButton.v_y, deepButton.v_w, deepButton.v_h);
    [lightButton setTitle:FYLocalizedString(@"sleep_watch_shallow_title", @"") forState:UIControlStateNormal];
    [lightButton setTitleColor:J_ColorFromHEX(0x333333) forState:UIControlStateNormal];
    lightButton.titleLabel.font = J_Font(12);
    [lightButton setImage:[UIImage qmui_imageWithColor:J_ColorFromHEX(0x70DA462) size:CGSizeMake(deepButton.v_h, deepButton.v_h) cornerRadius:0] forState:UIControlStateNormal];
    [lightButton jk_setImagePosition:LXMImagePositionLeft spacing:4];
    [self addSubview:lightButton];
    
    UIButton *awakeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    awakeButton.frame = J_Frame(lightButton.v_MaxX, deepButton.v_y, deepButton.v_w, deepButton.v_h);
    [awakeButton setTitle:FYLocalizedString(@"sleep_watch_sober_title", @"") forState:UIControlStateNormal];
    [awakeButton setTitleColor:J_ColorFromHEX(0x333333) forState:UIControlStateNormal];
    awakeButton.titleLabel.font = J_Font(12);
    [awakeButton setImage:[UIImage qmui_imageWithColor:J_ColorFromHEX(0x7c69B9) size:CGSizeMake(deepButton.v_h, deepButton.v_h) cornerRadius:0] forState:UIControlStateNormal];
    [awakeButton jk_setImagePosition:LXMImagePositionLeft spacing:4];
    [self addSubview:awakeButton];
}

- (CGFloat)widthOfLabelWithText:(NSString *)text{
    UILabel *tempLabel = [[UILabel alloc]init];
    tempLabel.font = J_Font(12);
    tempLabel.text = text;
    CGSize size = [tempLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, 20)];
    return size.width;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (ARTWatchHistoryCommonChartTitleView *)titleView{
    if (!_titleView) {
        _titleView = [[ARTWatchHistoryCommonChartTitleView alloc]initWithFrame:J_Frame(0, 0, self.v_w, 20)];
    }
    return _titleView;
}

@end
